/*
 * Copyright 2008 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.splink.library.queue {
	import org.splink.library.distributor.Distributor;

	/**
	 * @inheritDoc
	 * 
	 * @author Max Kugland
	 */
	internal class QableDecorator extends Distributor implements IQableDecorator {
		private var _isRunning : Boolean = false;
		private var _isComplete : Boolean = false;
		protected var _wrapped : IQable;
		
		/**
		 * @param q the <code>IQable</code> which is to be decorated
		 */
		public function QableDecorator(q : IQable) {
			_wrapped = q;
		}

		/**
		 * @inheritDoc
		 */
		final public function start() : void {
			_isComplete = false;
			_isRunning = true;
			
			registerListeners();			
			_wrapped.start();
		}

		/**
		 * @inheritDoc
		 */
		final public function stop() : void {
			_isRunning = false;
			_wrapped.stop();
		}

		/**
		 * @inheritDoc
		 */
		final public function reset() : void {
			_isRunning = false;
			_isComplete = false;
			_wrapped.reset();
		}

		private function registerListeners() : void {
			if(_wrapped) {
				_wrapped.register(QEvent.START, onStart);				_wrapped.register(QEvent.COMPLETE, onComplete);
				_wrapped.register(QEvent.ERROR, onError);
				_wrapped.register(QEvent.PROGRESS, onProgress);
			}
		}

		private function unregisterListeners() : void {
			if(_wrapped) {
				_wrapped.unregister(QEvent.START, onStart);
				_wrapped.unregister(QEvent.COMPLETE, onComplete);
				_wrapped.unregister(QEvent.ERROR, onError);
				_wrapped.unregister(QEvent.PROGRESS, onProgress);
			}
		}
		
		private function onStart(e : QEvent) : void {
			distribute(e);
		}
		
		private function onProgress(e : QEvent) : void {
			distribute(e);
		}

		private function onError(e : QEvent) : void {
			_isRunning = false;
			_isComplete = true;
			distribute(e);
		}

		private function onComplete(e : QEvent) : void {
			unregisterListeners();
			_isRunning = false;
			_isComplete = true;
			distribute(e);
		}

		/**
		 * @inheritDoc
		 */
		final public function get id() : String {
			return _wrapped.id;
		}

		/**
		 * @inheritDoc
		 */
		override public function finalize() : void {
			_isRunning = false;
			_wrapped.finalize();
			_wrapped = null;
		}

		/**
		 * @inheritDoc
		 */
		public function get isRunning() : Boolean {
			return _isRunning;
		}

		/**
		 * @inheritDoc
		 */
		public function isComplete() : Boolean {
			return _isComplete;
		}
		
		/**
		 * @inheritDoc
		 */
		public function getIQable() : IQable {
			return _wrapped;
		}
	}
}
